/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * 
 * Para habilitarlo en linux es en /etc/services/
 */
package gps.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 *
 * @author emojica
 */
public class Main {

    final int port = 5000;
    ServerSocket serverSocket;
    Socket socket;
    BufferedReader dataIn;

    public void listener() {
        try {
            serverSocket = new ServerSocket(port);

            socket = new Socket();
            System.out.println("Esperando una conexión:");

            while (true) {

                socket = serverSocket.accept();
                System.out.println("Un cliente se ha conectado.");

                dataIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                System.out.println("Confirmando conexion al cliente....");

                //Recepcion de mensaje
                System.out.println(dataIn.readLine());

                //cerrar conexionﬁ
                socket.close();
            }

        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
    }

    private void send() throws UnknownHostException, IOException {
        try (Socket skCliente = new Socket("localhost", 5000)) {
            InputStream aux = skCliente.getInputStream();
            DataInputStream flujo = new DataInputStream(aux);
            System.out.println(flujo.readUTF());
        }
    }
}
